package com.handpay.nocardpay.baselibrary.main.all.view.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.db.ormlite.DataBaseComponentModel;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.main.all.model.bean.component.ComponentRelationItemBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lyang on 2017/1/9.
 */

public class IconBaseAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private Context mContext;
    private ArrayList<String> data = null;
    private HashMap<String, FunctionInfo> maps = null;
    private boolean bShowAnim = false;

    public void setData(ArrayList<String> data) {
        ArrayList<String> newData = new ArrayList<String>();
        for(String itemCode:data){
            HashMap<String, FunctionInfo> maps = FunctionInfo.getFunctionInfos(mContext);
            FunctionInfo info = maps.get(itemCode);
            if (FunctionInfo.isExistComponent(itemCode)) {
                if (info.type.equals("1")){
                    newData.add(itemCode);
                }else {
                    DataBaseComponentModel mDBModel = new DataBaseComponentModel(mContext);
                    List<ComponentRelationItemBean> list = mDBModel.queryComponentRelationByParentItemCode(info.itemId);
                    boolean isExist = false;
                    for (ComponentRelationItemBean bean : list) {
                        if (FunctionInfo.isExistComponent(bean.getItemCode())) {
                            isExist = true;
                            break;
                        }
                    }
                    if (isExist){
                        newData.add(itemCode);
                    }
                }
            }
        }
        this.data = newData;
        this.maps = FunctionInfo.getFunctionInfos(mContext);
        notifyDataSetChanged();
    }


    public ArrayList<String> getData() {
        return data;
    }


    public IconBaseAdapter(Context context) {
        super();
        mContext = context;
        inflater = LayoutInflater.from(context);
        data = new ArrayList<String>();
//        maps = FunctionInfo.getFunctionInfos(mContext);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public int getGridViewItemView() {
        return R.layout.layout_all_function_item;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (position < data.size()) {
            if (position == 0 || convertView == null || convertView.getTag() == null) {
                convertView = inflater.inflate(getGridViewItemView(), null);
                holder = new ViewHolder();
                holder.imageView = (ImageView) convertView.findViewById(R.id.home_item_img);
                holder.itemsText = (TextView) convertView.findViewById(R.id.home_item_txt);
//                holder.roundView = convertView.findViewById(R.id.notice_round);
                // 设置标记
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            String id = data.get(position);
            FunctionInfo info = maps.get(id);
            holder.imageView.setImageResource(info.iconResId[0]);

//            if (bShowAnim) {
//                AnimatorSet animatorSet = new AnimatorSet();
//                ObjectAnimator scaleX = ObjectAnimator.ofFloat(holder.imageView, "scaleX", 1.0f, 0.9f, 1.0f);
//                ObjectAnimator scaleY = ObjectAnimator.ofFloat(holder.imageView, "scaleY", 1.0f, 0.9f, 1.0f);
//                animatorSet.setDuration(600);
//                animatorSet.setInterpolator(new DecelerateInterpolator());
//                animatorSet.play(scaleX).with(scaleY);
//                animatorSet.start();
//            }
            holder.itemsText.setText(info.title);

        }


        return convertView;
    }

    public void setShowAnimator(boolean bShowAnim) {
        this.bShowAnim = bShowAnim;
    }

    /**
     * ViewHolder类
     */
    static class ViewHolder {
        ImageView imageView;
        TextView itemsText;
        View roundView;
    }
}